//
//  WXSMotionSensor.m
//  SmartRecorderTest
//
//  Created by 王勇 on 2016/9/21.
//  Copyright © 2016年 王勇. All rights reserved.
//

#import "WXSMotionSensor.h"

#import "WXAlert.h"

#import "WXSSleepManager.h"


@implementation WXSMotionSensor

+(instancetype)sensor{
    static WXSMotionSensor *sensor = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sensor = [[WXSMotionSensor alloc] init];
    });
    return sensor;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.sensor = [[CMMotionManager alloc]init];
    }
    return self;
}
-(void)enableMotionSensor{
    if (![self.sensor isAccelerometerAvailable]) {
        [WXAlert showAlert:@"传感器不可用，睡眠结果会有所误差！" buttonAction:nil];
        return;
    }
    
    self.sensor.accelerometerUpdateInterval = 1.0/10.0;
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [self.sensor startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
        if (error == nil) {
//            NSString *xStr = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.x];
//            NSString *yStr = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.y];
//            NSString *zStr = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.z];
            dispatch_async(dispatch_get_main_queue(), ^{
//                self.xAxis.text = xStr;
//                self.yAxis.text = yStr;
//                self.zAxis.text = zStr;
            });
        }
    }];
    
    if (![self.sensor isGyroAvailable]) {
        NSLog(@"陀螺仪不可用！");
    }
    
    self.sensor.gyroUpdateInterval = 1.0;
    [self.sensor startGyroUpdatesToQueue:queue withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
                    [[NSRunLoop currentRunLoop] run];

        if (error == nil) {
            NSString *xStr = [NSString stringWithFormat:@"%.2f",gyroData.rotationRate.x];
            NSString *yStr = [NSString stringWithFormat:@"%.2f",gyroData.rotationRate.y];
            NSString *zStr = [NSString stringWithFormat:@"%.2f",gyroData.rotationRate.z];
            dispatch_async(dispatch_get_main_queue(), ^{
//                self.xRate.text = xStr;
//                self.yRate.text = yStr;
//                self.zRate.text = zStr;
            });
            
            //检测三轴的旋转速度
            if (fabs(gyroData.rotationRate.x) > 2.0 ||
                fabs(gyroData.rotationRate.y) > 2.0 ||
                fabs(gyroData.rotationRate.z) > 2.0) {
                NSLog(@"手机被移动了！");
            }
            
            //检测更大范围的三轴的旋转速度
            if (fabs(gyroData.rotationRate.x) > 3.0 ||
                fabs(gyroData.rotationRate.y) > 3.0 ||
                fabs(gyroData.rotationRate.z) > 3.0) {
                NSLog(@"移动幅度很大，判定为您已醒来！");
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[WXSSleepManager defaultManager]enterAwakeStatus:nil];
                });
//                NSNotification *awakeNotification = [[NSNotification alloc]initWithName:@"EnterAwakeStatusNotification" object:self userInfo:nil];
//                [[NSNotificationCenter defaultCenter]postNotification:awakeNotification];
            }
        }
    }];
}

-(void)disableMotionSensor{
    if ([self.sensor isGyroActive]) {
        [self.sensor stopGyroUpdates];
    }
    
    if ([self.sensor isAccelerometerActive]) {
        [self.sensor stopAccelerometerUpdates];
    }
}

@end
